package org.example.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.example.ZuulProperties;
import org.example.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

@Component
@Slf4j
public class LoginFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "post";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Autowired
    ZuulProperties zuulProperties;

    @Override
    public boolean shouldFilter() {
        return shouldFilter("login/login");
    }

    public boolean shouldFilter(String path) {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        // /damai-user/login/login  /user/login/login
        String routes = "";
        for (String loginRoute : zuulProperties.getLoginRoutes()) {
            if(routes.isBlank() == false){
                routes += "|";
            }
            routes += loginRoute;
        }
        String regex = "/(" + routes + ")/" + path;
        String servletPath = request.getServletPath();
        log.debug("servletPath = " + servletPath);
        log.debug("regex = " + regex);
        log.debug("servletPath.matches(regex) = " + servletPath.matches(regex));
        return servletPath.matches(regex);
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletResponse response = currentContext.getResponse();
        InputStream responseDataStream = currentContext.getResponseDataStream();
        byte[] bytes = new byte[1024];
        try {
            int count = responseDataStream.read(bytes);
            String json = new String(bytes, 0, count);
            log.debug("json = " + json);
            Map result = new ObjectMapper().readValue(json, Map.class);
            Object code = result.get("code");
            if("1".equals(code+"")){
                Map user = (Map) result.get("data");
                String jwt = JwtUtil.createJWT(99999, user);
                Cookie cookie = new Cookie("loginToken" , jwt);
                cookie.setPath("/");
                cookie.setMaxAge(60 * 30);
                response.addCookie(cookie);
            }
            currentContext.getResponse().setContentType("application/json;charset=utf-8");
            currentContext.setResponseBody(json);
        } catch (IOException e) {
            throw new RuntimeException("获取用户信息失败",e);
        }
        return null;
    }
}
